<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-descriptions class="margin-top" title="订单详情" :column="3" :size="size" border>
        <el-descriptions-item label="订单编号">{{formData.orderId}}</el-descriptions-item>
        <el-descriptions-item label="客户手机">{{formData.phonenumber}}</el-descriptions-item>
        <el-descriptions-item label="进仓件数">{{formData.warehouseNumber}}</el-descriptions-item>
        <el-descriptions-item label="品名/型号" :span="3">{{formData.productNameMode}}</el-descriptions-item>
        <el-descriptions-item label="货物类型">{{getDictLabel(DICT_TYPE.GOODS_NATURE, formData.goodsNature)}}</el-descriptions-item>
        <el-descriptions-item v-if="formData.goodsNature == 1" label="需要特殊防护">{{getDictLabel(DICT_TYPE.BUS_TRUE_FALSE, formData.specialProtection)}}</el-descriptions-item>
        <el-descriptions-item v-if="formData.goodsNature == 1" label="自带危包">{{getDictLabel(DICT_TYPE.BUS_TRUE_FALSE, formData.comesDangerousBag)}}</el-descriptions-item>
        <el-descriptions-item label="主订单号">{{formData.mainOrderId}}</el-descriptions-item>
        <el-descriptions-item label="目的港">{{formData.unloadingPort}}</el-descriptions-item>
    </el-descriptions>
    <div style="color:#303133;font-size: 16px;font-weight: bold;padding:15px 0;">航班到港</div>
    <el-input v-model="formData.reminder" style="width: 100%" :rows="6" type="textarea" placeholder="确认货物到港，告知用户到港情况，以及相关的提货信息"/>
    <template #footer>
      <el-button @click="submitForm(6)" type="primary" :disabled="formLoading">保 存</el-button>
      <el-button @click="submitForm(7)" type="primary" :disabled="formLoading">完成</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE, getDictLabel } from '@/utils/dict'
import { CargoInfoApi, CargoInfoVO } from '@/api/business/cargoinfo'
import type { ComponentSize } from 'element-plus'

/** 货物资料 表单 */
defineOptions({ name: 'ArrivalForm' })

const size = ref<ComponentSize>('default')

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  orderId: '',
  warehouseNumber: undefined,
  productNameMode: undefined,
  goodsNature: '0',
  specialProtection: 0,
  comesDangerousBag: 0,
  mainOrderId: '',
  unloadingPort: '',
  phonenumber:'',
  status:0,
  reminder:""
})

/** 打开弹窗 */
const open = async (id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '航班到港'
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CargoInfoApi.getCargoInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async (status:number) => {
  if(!formData.value.reminder){
    message.error('请输入到港信息！');
    return;
  }
  // 提交请求
  formLoading.value = true
  try {
      await CargoInfoApi.addArrivalInfo({
        cargoId: formData.value.id,
        reminder: formData.value.reminder,
        status: status
      })
      message.success('保存成功')
    
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}


</script>
